package com.uk.ui.kontrata;

import java.util.Collection;
import java.util.List;

import com.uk.data.containers.KontrataContainer;
import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.BashkiKomuna;
import com.uk.data.entities.Kontrata;
import com.uk.data.entities.Perdorimi;
import com.uk.data.entities.Qarku;
import com.uk.data.entities.Rrethi;
import com.uk.ui.CHorizontalSplitPanel;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.SplitPanel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window.Notification;

public class KontrataPanel  extends CHorizontalSplitPanel implements ItemClickListener, ValueChangeListener{

	private IFaturaBean faturaBean;
	
	private KontrataNavigationTree kontrataNavigationTree = new KontrataNavigationTree(this);
	
	private KontrataContainer dataSource;
	private BeanItemContainer<Perdorimi> perdorimiContainer;
	private BeanItemContainer<Qarku> qarkuContainer;

	private KontrataListView kontrataListView = null;
	private KontrataList kontrataList = null;
	private KontrataForm kontrataForm = null;
	private KontrataDebitorListView kontrataDebitorListView = null;
	private KontrataSearchView kontrataSearchView = null;
	
	
	
	public KontrataPanel(IFaturaBean faturaBean) {
		

		this.faturaBean = faturaBean;
		this.dataSource = KontrataContainer.createFromEjb(faturaBean);
		this.perdorimiContainer = new BeanItemContainer<Perdorimi>(faturaBean.queryPerdorimi());
		this.qarkuContainer = new BeanItemContainer<Qarku>(faturaBean.queryQarku());
		this.setSizeFull();
		this.setSplitPosition(150, SplitPanel.UNITS_PIXELS);
		this.setFirstComponent(this.kontrataNavigationTree);
		this.setSecondComponent(this.getKontrataListView());
		
	}

	public KontrataContainer getDataSource(){
		return dataSource;
	}
	
	
	
	@Override
	public void itemClick(ItemClickEvent event) {
		
		if (event.getSource() == this.kontrataNavigationTree){
			Object itemId = event.getItemId();
			if (itemId != null) {
				if (KontrataNavigationTree.LIST.equals(itemId)){
					
					this.dataSource = KontrataContainer.createFromEjb(faturaBean);
					setSecondComponent(getKontrataListView());
					this.kontrataForm.setItemDataSource(null);
					
				}else if (KontrataNavigationTree.ADD.equals(itemId)){
					
					kontrataForm.addKontrate();
				}
				else if (KontrataNavigationTree.SEARCH.equals(itemId)){
					
					setSecondComponent(this.getKontrataSearchView());
				}
				else if (KontrataNavigationTree.LIST_DEBITOR.equals(itemId)){
					
					setSecondComponent(this.getKontrataDebitorListView());
				}
				
			}
		}
		
	}

	@Override
	public void valueChange(ValueChangeEvent event) {
		Property property = event.getProperty();
		if (property == kontrataList){
			Item item = kontrataList.getItem(kontrataList.getValue());
			
			if (item != kontrataForm.getItemDataSource()){
				kontrataForm.setItemDataSource(item);
			}
		}
		
	}

	public KontrataListView getKontrataListView() {
		if(kontrataListView == null){
			this.kontrataList = new KontrataList(this);
			this.kontrataForm = new KontrataForm(this);
			this.kontrataListView = new KontrataListView(kontrataList, kontrataForm);
		}
		else {
			this.kontrataList  =new KontrataList(this);
			this.kontrataListView.setKontrataList(this.kontrataList);
		}
			
		return kontrataListView;
	}
	
	public KontrataSearchView getKontrataSearchView(){
		if (kontrataSearchView == null) this.kontrataSearchView = new KontrataSearchView(this);
		
		return kontrataSearchView;
		
	}

	public KontrataDebitorListView getKontrataDebitorListView(){
		if (kontrataDebitorListView == null) this.kontrataDebitorListView = new KontrataDebitorListView(this);
		return kontrataDebitorListView;
	}
	public BeanItemContainer<Perdorimi> getPerdorimiContainer() {
		return perdorimiContainer;
	}

	public BeanItemContainer<Qarku> getQarkuContainer() {
		return qarkuContainer;
	}

	public BeanItemContainer<Rrethi> getRrethiContainer(Qarku qarku) {
		Collection<Rrethi>  rrethiList = faturaBean.queryRrethi(qarku);
		if (rrethiList == null || rrethiList.size() == 0 ) return null;
		else return new BeanItemContainer<Rrethi>(rrethiList);
	
	}

	public BeanItemContainer<BashkiKomuna> getBashkiKomunaContainer( Rrethi rrethi) {
		Collection<BashkiKomuna> bashkiKomunaList = faturaBean.queryBashkiKomuna(rrethi);
		if (bashkiKomunaList == null || bashkiKomunaList.size() == 0) return null;
		else return new BeanItemContainer<BashkiKomuna>(bashkiKomunaList);
	
	}

	public IFaturaBean getFaturaBean() {
		return faturaBean;
	}

	public void search(String emriVal, String mbiemriVal) {
		
		try {
			List<Kontrata> lista  = this.faturaBean.queryKontrata(emriVal, mbiemriVal, 1000);
			if (lista == null || lista.isEmpty())
				this.dataSource = new KontrataContainer();
			else
				
				this.dataSource = new KontrataContainer(this.faturaBean.queryKontrata(emriVal, mbiemriVal, 1000));
			 
			 setSecondComponent(this.getKontrataListView());
			 getWindow().showNotification(
						"Numri i rezultateve te kerkimit:  "
								+ getDataSource().size() + " item(s).",
						Notification.TYPE_TRAY_NOTIFICATION);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	

	
}
